package com.vken.blog.service.impl;

import java.util.List;

import com.vken.blog.dao.*;
import com.vken.blog.model.*;
import com.vken.blog.service.CategoryManager;

public class CategoryManagerImpl implements CategoryManager {
	private CategoryDao categoryDao;
	private BlogDao blogDao;
	private EntryDao entryDao;

	public void setCategoryDao(CategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}

	public void setBlogDao(BlogDao blogDao) {
		this.blogDao = blogDao;
	}

	public void setEntryDao(EntryDao entryDao) {
		this.entryDao = entryDao;
	}

	public void insert(Category category, Integer blogId) {
		category.setBlog(blogDao.findById(blogId));
		categoryDao.save(category);
	}

	public void update(Category category) {
		categoryDao.update(category);
	}

	public void delete(Integer id, Integer targetId) {
		List<Entry> list = entryDao.getEntrysByCategoryId(id);
		Category category = categoryDao.findById(targetId);
		if (category != null) {
			for (Entry e : list) {
				e.setCategory(category);
				entryDao.update(e);
			}
			categoryDao.delete(categoryDao.findById(id));
		}
	}

	public List<Category> list(Integer blogId) {
		return categoryDao.getCategorysByBlogId(blogId);
	}

	public Category get(Integer id) {
		return categoryDao.findById(id);
	}
}
